import javax.swing.*;
import java.awt.*;

/**
 * 最基本的渲染类，其派生类实现具体内容
 */
public abstract class Rendering {
    public int gameMapWidth; // 地图宽度
    public int gameMapHeight; // 地图高度
    public static int[][] map;
    JFrame gameLevelFrme;
    JPanel informatioPanelUI;
    JPanel gameRunInformationUI;
    JPanel playerInformPanel;
    JPanel targetJPanel;
    private final int JFRAMEWIDTH = 1300; // 窗口最大宽度
    private final int JFRAMEHEIGHT = 1000; // 窗口最大高度
    /**
     * 游戏运行主面板x轴开始绘制的位置
     */
    public int drawMainGamePanelWidthStartX = JFRAMEWIDTH / 10 + (int) (JFRAMEWIDTH * 0.2) + 5;
    /**
     * 游戏运行主面板y轴结束绘制的位置(最大高度)
     */
    public int drawMainGamePanelWidthEndY = JFRAMEHEIGHT - (int) (JFRAMEHEIGHT * 0.16);
    /**
     * 游戏运行窗口最大的宽度
     */
    public int drawMainGamePanelMaxWidth = drawMainGamePanelWidthEndY;

    /**
     * 初始化游戏窗口
     */
    public abstract void init();

    // /**
    // * 各种图片的路径
    // * 哈希表存储可能更好
    // */
    // public class imagesSrc {
    // String enermy1 = "images/enermy1.jpg";
    // String enermy2 = "images/enermy2.jpg";
    // String house1 = "images/house1.jpg";
    // String npc1 = "images/npc1.jpg";
    // String player = "images/player.jpg";
    // String school = "images/school.jpg";
    // String tree1 = "images/tree1.jpg";
    // String wall = "images/wall.png";
    // String wq1 = "images/wq1.jpg";
    // String undefinedArea = "images/unfined.jpg";
    // }

    /**
     * 地图区块的所有类型
     */
    public static enum mapAreaTypes {
        undefinedArea, // 0
        player, // 1
        enermy1, // 2
        tree1, // 3
        house1, // 4
        school, // 5
        wall, // 6
        npc1, // 7
        out, // 8
        enermy2; // 9

        /**
         * 图片的路径
         */
        /* 注意路径必须按照枚举顺序 */
        public static String[] areaImagesSrc = new String[] {
                "images/unfined.jpg",
                "images/player.jpg",
                "images/enermy1.jpg",
                "images/tree1.jpg",
                "images/house1.jpg",
                "images/school.jpg",
                "images/wall.png",
                "images/npc1.jpg",
                "images/unfined.jpg",
                "images/enermy2.jpg"
        };
    }

    /**
     * 初始化基本游戏视图
     */
    public Rendering(String title) {
        iniGameJframe(title);
    }

    /**
     * 在指定位置设置图片
     * 
     * @param gamePanel 游戏视图
     * @param n         位置，从0开始
     * @param src       图片的位置
     */
    public void drawArea(JPanel gamePanel, int n, String src) {
        Component c = gamePanel.getComponent(n);
        if (c instanceof JLabel) {
            // System.out.println(n);
            JLabel jLabel = (JLabel) c;
            jLabel.setSize(drawMainGamePanelWidthEndY / map.length, drawMainGamePanelWidthEndY / map.length);
            ImageIcon imageIcon = new ImageIcon(src);
            imageIcon.setImage(
                    imageIcon.getImage().getScaledInstance(jLabel.getWidth(), jLabel.getHeight(), Image.SCALE_DEFAULT));
            jLabel.setIcon(imageIcon);
        }
    }

    /**
     * 在指定位置设置图片
     * 
     * @param gamePanel 游戏视图
     * @param position  位置
     * @param mapWidth  地图宽度
     * @param mapHeight 地图高度
     * @param src       图片位置
     */
    public void drawArea(JPanel gamePanel, int[] position, String src) {
        int n = position[0] * gameMapHeight + position[1];
        drawArea(gamePanel, n, src);
    }

    /**
     * 获取指定位置的类型
     * 
     * @param position 指定位置(行列)
     * @return 区块的类型
     */
    public int getAreaType(int[] position) {
        return map[position[0] - 1][position[1] - 1];
    }

    /**
     * 获取指定位置的类型
     * 
     * @param n 指定位置
     * @return 区块的类型
     */
    public int getAreaType(int n) {
        int col = n % gameMapWidth;
        int row = (int) Math.ceil(n * 1.0 / gameMapWidth);
        return map[row - 1][col - 1];
    }

    /**
     * 初始化游戏视图
     */
    private void iniGameJframe(String title) {
        /* 初始化游戏窗口 */
        gameLevelFrme = new JFrame(title);
        gameLevelFrme.setSize(JFRAMEWIDTH, JFRAMEHEIGHT);
        gameLevelFrme.setLayout(null);
        /* 初始化游戏信息面板 */
        informatioPanelUI = new JPanel();
        informatioPanelUI.setLayout(null);
        informatioPanelUI.setBounds(0, 0, JFRAMEWIDTH / 10, (int) (JFRAMEHEIGHT * (1 - 0.12)));
        informatioPanelUI.setBackground(Color.green);
        /* 初始化游戏运行状态面板 */
        gameRunInformationUI = new JPanel();
        gameRunInformationUI.setLayout(null);
        gameRunInformationUI.setBounds(0, (int) (JFRAMEHEIGHT * (1 - 0.12)), JFRAMEWIDTH, (int) (JFRAMEHEIGHT * 0.16));
        gameRunInformationUI.setBackground(Color.red);
        /* 初始化玩家信息面板 */
        playerInformPanel = new JPanel();
        playerInformPanel.setLayout(null);
        playerInformPanel.setBounds(JFRAMEWIDTH / 10, 0, (int) (JFRAMEWIDTH * 0.2),
                (int) (JFRAMEHEIGHT * (1 - 0.12)) / 2);
        playerInformPanel.setBackground(Color.blue);
        /* 初始化目标信息面板 */
        targetJPanel = new JPanel();
        targetJPanel.setLayout(null);
        targetJPanel.setBounds(JFRAMEWIDTH / 10, (JFRAMEHEIGHT - (int) (JFRAMEHEIGHT * 0.16)) / 2,
                (int) (JFRAMEWIDTH * 0.2),
                (int) (JFRAMEHEIGHT * (1 - 0.12)) / 2);
        targetJPanel.setBackground(Color.yellow);
        /* 把ui添加进游戏 */
        gameLevelFrme.add(targetJPanel);
        gameLevelFrme.add(gameRunInformationUI);
        gameLevelFrme.add(playerInformPanel);
        gameLevelFrme.add(informatioPanelUI);
        gameLevelFrme.setVisible(true);
    }

    /**
     * 初始化地图,通过每个数字，适用于最大区块小于10的地图
     * 
     * @param mapString 字符串的地图
     * @param n         地图大小,默认正方形
     */
    public void iniMapArrayByAllNum(String mapString, int n) {
        int raw = 0, col = 0;
        map = new int[n][n];
        for (int i = 0; i < mapString.length(); i++) {
            char a = mapString.charAt(i);
            if (a >= '0' && a <= '9') {
                map[raw][col++] = a - '0';
                if (a - '0' == mapAreaTypes.player.ordinal()) {
                    Player.setPosition(new int[] { raw, col });
                }
                if (col == n) {
                    col = 0;
                    raw++;
                }
            }
        }
        gameMapWidth = gameMapHeight = n;
    }

    /**
     * 初始化地图,通过逗号分割地图，全部适用
     * 
     * @param mapString 字符串的地图,区块逗号隔开
     * @param n         地图大小,默认正方形
     */
    /* 后面加的，开始游戏设想的时候设想到0-9居然会不够用 */
    public void iniMapArrayByComma(String mapString, int n) {
        int raw = 0, col = 0;
        map = new int[n][n];
        mapString = mapString.replace("\n", "");
        String[] mapStrings = mapString.split(",");
        for (int i = 0; i < mapStrings.length; i++) {
            int a = Integer.parseInt(mapStrings[i]);
            map[raw][col++] = a;
            if (col == n) {
                col = 0;
                raw++;
            }
        }
        if (raw != col && raw != n - 1 && col != n - 1) {
            throw new Error("地图初始化错误");
        }
        gameMapWidth = gameMapHeight = n;
    }

    /**
     * 初始化游戏区域，并绘制图片
     * 
     * @param gamePanel 需要绘制的窗口
     */
    public void iniMapArea(JPanel gamePanel) {
        /* 设置图片载体JLabel */
        for (int i = 0; i < gameMapHeight * gameMapWidth; i++) {
            JLabel jLabel = new JLabel();
            jLabel.setBackground(Color.white);
            gamePanel.add(jLabel);
        }
        /* 设置图片 */
        for (int raw = 0; raw < gameMapHeight; raw++) {
            for (int col = 0; col < gameMapWidth; col++) {
                try {
                    int a = map[raw][col];
                    String src = mapAreaTypes.areaImagesSrc[a];
                    drawArea(gamePanel, new int[] { raw, col }, src);
                } catch (IndexOutOfBoundsException e) {
                    System.out.println("地图初始化错误:" + e.getMessage());
                }
            }
        }
        /* 废弃方案，繁琐且增添区块麻烦 */
        // /* 设置图片 */
        // imagesSrc imaSrc = new imagesSrc();
        // for (int raw = 0; raw < gameMapHeight; raw++) {
        // for (int col = 0; col < gameMapWidth; col++) {
        // int a = map[raw][col];
        // String src = "-1";
        // if (a == mapAreaTypes.undefinedArea.ordinal()) {
        // src = imaSrc.undefinedArea;
        // } else if (a == mapAreaTypes.player.ordinal()) {
        // src = imaSrc.player;
        // } else if (a == mapAreaTypes.enermy1.ordinal()) {
        // src = imaSrc.enermy1;
        // } else if (a == mapAreaTypes.tree1.ordinal()) {
        // src = imaSrc.tree1;
        // } else if (a == mapAreaTypes.house1.ordinal()) {
        // src = imaSrc.house1;
        // } else if (a == mapAreaTypes.school.ordinal()) {
        // src = imaSrc.school;
        // } else if (a == mapAreaTypes.wall.ordinal()) {
        // src = imaSrc.wall;
        // } else if (a == mapAreaTypes.npc1.ordinal()) {
        // src = imaSrc.npc1;
        // } else if (a == mapAreaTypes.out.ordinal()) {
        // src = imaSrc.undefinedArea;
        // } else if (a == mapAreaTypes.enermy2.ordinal()) {
        // src = imaSrc.enermy2;
        // }
        // if (!src.equals("-1")) {
        // drawArea(gamePanel, new int[] { raw, col }, src);
        // } else {
        // throw new Error("绘制地图出错");
        // }
        // }
        // }
    }
}